﻿using System;
using RootMotion.FinalIK;
using UnityEngine;
using UnityEngine.VR;

namespace RootMotion.Demos
{
	// Token: 0x020000CD RID: 205
	[RequireComponent(typeof(FullBodyBipedIK))]
	public class VRAimingController : MonoBehaviour
	{
		// Token: 0x1700008B RID: 139
		// (get) Token: 0x06000616 RID: 1558 RVA: 0x00005E55 File Offset: 0x00004055
		public Transform cam
		{
			get
			{
				return this.animatorController.cam;
			}
		}

		// Token: 0x1700008C RID: 140
		// (get) Token: 0x06000617 RID: 1559 RVA: 0x00005E62 File Offset: 0x00004062
		public Transform characterController
		{
			get
			{
				return this.animatorController.characterController;
			}
		}

		// Token: 0x06000618 RID: 1560 RVA: 0x00038174 File Offset: 0x00036374
		public void Start()
		{
			this.ik = base.GetComponent<FullBodyBipedIK>();
			this.poserLeftHand = this.ik.references.leftHand.GetComponent<Poser>();
			this.poserRightHand = this.ik.references.rightHand.GetComponent<Poser>();
			IKSolverFullBodyBiped solver = this.ik.solver;
			solver.OnPostUpdate = (IKSolver.UpdateDelegate)Delegate.Combine(solver.OnPostUpdate, new IKSolver.UpdateDelegate(this.AfterFBBIK));
			this.lastWeight = this.weight;
			this.SetHandedness(this.handedness);
			this.defaultWeaponsAnchorLocalPosition = this.weaponsAnchor.localPosition;
			this.weaponsPivotLocalPosition = this.weaponsPivot.localPosition;
			this.pivotRelativePosition = this.pivotMotionTarget.InverseTransformPoint(this.weaponsPivot.position);
			this.cameraPosition = this.TargetsCameraPosition();
			this.lastCharacterPosition = this.characterController.position;
		}

		// Token: 0x06000619 RID: 1561 RVA: 0x00038264 File Offset: 0x00036464
		public void LateUpdate()
		{
			this.cameraPosition += this.characterController.position - this.lastCharacterPosition;
			this.lastCharacterPosition = this.characterController.position;
			this.cameraPosition = Vector3.Lerp(this.cameraPosition, this.TargetsCameraPosition(), Time.deltaTime * this.lerpSpeed);
			if (this.weight <= 0f && this.lastWeight <= 0f)
			{
				return;
			}
			this.lastWeight = this.weight;
			float t = this.animatorController.velocity.magnitude * this.pivotMotionWeight;
			this.weaponsPivot.position = Vector3.Lerp(this.weaponsPivot.position, Vector3.Lerp(this.weaponsPivot.parent.TransformPoint(this.weaponsPivotLocalPosition), this.pivotMotionTarget.TransformPoint(this.pivotRelativePosition), t), Time.deltaTime * this.pivotMotionSmoothSpeed);
			if (Input.GetKeyDown(KeyCode.H))
			{
				this.SetHandedness((this.handedness != VRAimingController.Handedness.Right) ? VRAimingController.Handedness.Right : VRAimingController.Handedness.Left);
			}
			this.defaultWeaponsAnchorLocalPosition.x = ((this.handedness != VRAimingController.Handedness.Right) ? (-this.sideOffset) : this.sideOffset);
			this.weaponsAnchor.localPosition = Vector3.Lerp(this.weaponsAnchor.localPosition, this.defaultWeaponsAnchorLocalPosition, Time.deltaTime * this.lerpSpeed);
			if (this.currentWeapon != null && Input.GetMouseButtonDown(0))
			{
				this.currentWeapon.Fire();
				this.weaponsAnchor.localPosition += this.currentWeapon.recoilDirection + UnityEngine.Random.insideUnitSphere * this.currentWeapon.recoilDirection.magnitude * UnityEngine.Random.value * this.currentWeapon.recoilRandom;
				this.aimVel.x = this.aimVel.x - (this.currentWeapon.recoilAngleVertical + this.currentWeapon.recoilAngleVertical * UnityEngine.Random.value * this.currentWeapon.recoilRandom);
				float num = this.currentWeapon.recoilAngleHorizontal * UnityEngine.Random.value;
				if (UnityEngine.Random.value > 0.5f)
				{
					num = -num;
				}
				this.aimVel.y = this.aimVel.y + (num + num * UnityEngine.Random.value * this.currentWeapon.recoilRandom);
			}
			foreach (VRAimingController.BoneRotationOffset boneRotationOffset in this.targets.boneRotationOffsets)
			{
				boneRotationOffset.transform.localRotation = Quaternion.Euler(boneRotationOffset.value * this.weight) * boneRotationOffset.transform.localRotation;
			}
			bool mouseButton = Input.GetMouseButton(1);
			float target = (!mouseButton) ? 0f : 1f;
			this.aimWeight = Mathf.MoveTowards(this.aimWeight, target, Time.deltaTime * 3f);
			this.RotateWeapon(Input.GetAxis("Mouse X") * (1f - this.aimWeight), Input.GetAxis("Mouse Y") * (1f - this.aimWeight));
			if (mouseButton)
			{
				this.weaponsPivot.position = Vector3.Lerp(this.weaponsPivot.position, this.cameraPosition, this.aimWeight);
				this.weaponsAnchor.localPosition = Vector3.Lerp(this.weaponsAnchor.localPosition, new Vector3(0f, this.weaponsAnchor.localPosition.y, this.aimZ), this.aimWeight);
				this.weaponsPivot.rotation = Quaternion.Lerp(this.weaponsPivot.rotation, Quaternion.LookRotation(this.cam.forward), Time.deltaTime * this.lerpSpeed);
			}
			Vector3 vector = Vector3.Project(this.weaponsAnchor.position - this.TargetsCameraPosition(), this.cam.forward);
			if (Vector3.Dot(vector, this.cam.forward) < 0f)
			{
				this.weaponsAnchor.position -= vector;
			}
			this.ik.solver.leftHandEffector.position = this.targets.leftHand.position;
			this.ik.solver.rightHandEffector.position = this.targets.rightHand.position;
			this.ik.solver.leftHandEffector.positionWeight = this.weight;
			this.ik.solver.rightHandEffector.positionWeight = this.weight;
			this.ik.solver.leftArmChain.bendConstraint.bendGoal = this.targets.bendGoalLeftArm;
			this.ik.solver.rightArmChain.bendConstraint.bendGoal = this.targets.bendGoalRightArm;
			this.ik.solver.leftArmChain.bendConstraint.weight = this.weight;
			this.ik.solver.rightArmChain.bendConstraint.weight = this.weight;
			this.poserLeftHand.weight = this.weight;
			this.poserRightHand.weight = this.weight;
			this.animatorController.RotateCharacter(this.weaponsAnchor.forward, this.animatorController.maxViewAngle, this.weaponsPivot);
			this.TwistSpine();
		}

		// Token: 0x0600061A RID: 1562 RVA: 0x00038828 File Offset: 0x00036A28
		public void RotateWeapon(float horAdd, float vertAdd)
		{
			Vector3 vector = new Vector3(-vertAdd, horAdd, 0f);
			this.aimRandom = Vector3.Lerp(this.aimRandom, UnityEngine.Random.onUnitSphere, Time.deltaTime);
			vector += this.aimRandom * 0.25f;
			this.aimVel = Vector3.Lerp(this.aimVel, vector, Time.deltaTime * 20f);
			Vector3 forward = this.weaponsPivot.forward;
			forward.y = 0f;
			Quaternion rotation = Quaternion.AngleAxis(this.aimVel.x, Quaternion.LookRotation(forward) * Vector3.right);
			Quaternion rotation2 = Quaternion.AngleAxis(this.aimVel.y, Vector3.up);
			Vector3 point = Vector3.RotateTowards(forward, rotation * this.weaponsPivot.forward, this.aimVerticalLimit * 0.0174532924f, 1f);
			this.weaponsPivot.rotation = Quaternion.LookRotation(rotation2 * point, Vector3.up);
		}

		// Token: 0x1700008D RID: 141
		// (get) Token: 0x0600061B RID: 1563 RVA: 0x00005E6F File Offset: 0x0000406F
		public VRAimingController.Targets targets
		{
			get
			{
				if (this.handedness == VRAimingController.Handedness.Right)
				{
					return this.rightHandedTargets;
				}
				return this.leftHandedTargets;
			}
		}

		// Token: 0x0600061C RID: 1564 RVA: 0x0003892C File Offset: 0x00036B2C
		public Vector3 TargetsCameraPosition()
		{
			float d = (InputTracking.GetLocalPosition(VRNode.LeftEye) - InputTracking.GetLocalPosition(VRNode.RightEye)).magnitude * 0.5f;
			Vector3 vector = Vector3.right * d;
			if (this.handedness == VRAimingController.Handedness.Left)
			{
				vector = -vector;
			}
			return this.cam.position + this.cam.rotation * vector;
		}

		// Token: 0x0600061D RID: 1565 RVA: 0x0003899C File Offset: 0x00036B9C
		public void SetHandedness(VRAimingController.Handedness h)
		{
			this.handedness = h;
			this.poserLeftHand.poseRoot = this.targets.leftHand;
			this.poserRightHand.poseRoot = this.targets.rightHand;
			this.poserLeftHand.AutoMapping();
			this.poserRightHand.AutoMapping();
		}

		// Token: 0x0600061E RID: 1566 RVA: 0x000389F8 File Offset: 0x00036BF8
		public void TwistSpine()
		{
			if (this.spineTwistWeight <= 0f)
			{
				return;
			}
			Vector3 forward = this.weaponsAnchor.forward;
			forward.y = 0f;
			Quaternion b = Quaternion.FromToRotation(base.transform.forward, forward);
			foreach (VRAimingController.BoneRotationOffset boneRotationOffset in this.targets.boneRotationOffsets)
			{
				boneRotationOffset.transform.rotation = Quaternion.Lerp(Quaternion.identity, b, 1f / (float)this.targets.boneRotationOffsets.Length * this.spineTwistWeight) * boneRotationOffset.transform.rotation;
			}
		}

		// Token: 0x0600061F RID: 1567 RVA: 0x00038AB4 File Offset: 0x00036CB4
		public void AfterFBBIK()
		{
			if (this.weight <= 0f)
			{
				return;
			}
			this.ik.references.leftHand.rotation = Quaternion.Lerp(this.ik.references.leftHand.rotation, this.targets.leftHand.rotation, this.weight);
			this.ik.references.rightHand.rotation = Quaternion.Lerp(this.ik.references.rightHand.rotation, this.targets.rightHand.rotation, this.weight);
		}

		// Token: 0x06000620 RID: 1568 RVA: 0x00005E89 File Offset: 0x00004089
		public void OnDestroy()
		{
			if (this.ik != null)
			{
				IKSolverFullBodyBiped solver = this.ik.solver;
				solver.OnPostUpdate = (IKSolver.UpdateDelegate)Delegate.Remove(solver.OnPostUpdate, new IKSolver.UpdateDelegate(this.AfterFBBIK));
			}
		}

		// Token: 0x06000621 RID: 1569 RVA: 0x00005EC8 File Offset: 0x000040C8
		public float ClampAngle(float angle, float min, float max)
		{
			if (angle < -360f)
			{
				angle += 360f;
			}
			if (angle > 360f)
			{
				angle -= 360f;
			}
			return Mathf.Clamp(angle, min, max);
		}

		// Token: 0x04000521 RID: 1313
		[Header("Component References")]
		public VRAnimatorController animatorController;

		// Token: 0x04000522 RID: 1314
		[Tooltip("Which weapon is the character holding at this time?")]
		public WeaponBase currentWeapon;

		// Token: 0x04000523 RID: 1315
		[Range(0f, 1f)]
		[Tooltip("The master weight of aiming.")]
		[Header("Weights")]
		public float weight = 1f;

		// Token: 0x04000524 RID: 1316
		[Range(0f, 1f)]
		[Tooltip("The weight of twisting the spine to better hold the weapons")]
		public float spineTwistWeight = 1f;

		// Token: 0x04000525 RID: 1317
		[Header("Hands")]
		[Tooltip("Which hand holds the weapon?")]
		public VRAimingController.Handedness handedness;

		// Token: 0x04000526 RID: 1318
		[Tooltip("How far left/right to offset the weapons?")]
		public float sideOffset = 0.1f;

		// Token: 0x04000527 RID: 1319
		[Tooltip("Various references and settings for left handed weapons.")]
		public VRAimingController.Targets leftHandedTargets;

		// Token: 0x04000528 RID: 1320
		[Tooltip("Various references and settings for right handed weapons.")]
		public VRAimingController.Targets rightHandedTargets;

		// Token: 0x04000529 RID: 1321
		[Header("Weapon Positioning")]
		[Tooltip("The Transform that rotates the weapon.")]
		public Transform weaponsPivot;

		// Token: 0x0400052A RID: 1322
		[Tooltip("Child of weaponsPivot, parent of all weapons.")]
		public Transform weaponsAnchor;

		// Token: 0x0400052B RID: 1323
		[Tooltip("Weapons will inherit motion from that Transform.")]
		public Transform pivotMotionTarget;

		// Token: 0x0400052C RID: 1324
		[Tooltip("Speed of various position/rotation interpolations.")]
		public float lerpSpeed = 8f;

		// Token: 0x0400052D RID: 1325
		[Tooltip("The smoothing speed of inheriting motion from the pivotMotionTarget.")]
		public float pivotMotionSmoothSpeed = 5f;

		// Token: 0x0400052E RID: 1326
		[Range(0f, 1f)]
		[Tooltip("The weight of inheriting motion from the pivotMotionTarget,")]
		public float pivotMotionWeight = 0.5f;

		// Token: 0x0400052F RID: 1327
		[Range(0f, 90f)]
		[Tooltip("The limit of up/down rotation for the weapons.")]
		public float aimVerticalLimit = 80f;

		// Token: 0x04000530 RID: 1328
		[Tooltip("Local Z position of the weapons anchor when the weapon is locked to the camera (while holding RMB).")]
		public float aimZ = 0.05f;

		// Token: 0x04000531 RID: 1329
		public FullBodyBipedIK ik;

		// Token: 0x04000532 RID: 1330
		public float lastWeight;

		// Token: 0x04000533 RID: 1331
		public Poser poserLeftHand;

		// Token: 0x04000534 RID: 1332
		public Poser poserRightHand;

		// Token: 0x04000535 RID: 1333
		public Vector3 pivotRelativePosition;

		// Token: 0x04000536 RID: 1334
		public Vector3 weaponsPivotLocalPosition;

		// Token: 0x04000537 RID: 1335
		public Vector3 defaultWeaponsAnchorLocalPosition;

		// Token: 0x04000538 RID: 1336
		public Vector3 aimVel;

		// Token: 0x04000539 RID: 1337
		public Vector3 aimRandom;

		// Token: 0x0400053A RID: 1338
		public float x;

		// Token: 0x0400053B RID: 1339
		public float y;

		// Token: 0x0400053C RID: 1340
		public float aimWeight;

		// Token: 0x0400053D RID: 1341
		public Vector3 cameraPosition;

		// Token: 0x0400053E RID: 1342
		public Vector3 lastCharacterPosition;

		// Token: 0x020000CE RID: 206
		[Serializable]
		public struct Targets
		{
			// Token: 0x0400053F RID: 1343
			public Transform leftHand;

			// Token: 0x04000540 RID: 1344
			public Transform rightHand;

			// Token: 0x04000541 RID: 1345
			public Transform bendGoalLeftArm;

			// Token: 0x04000542 RID: 1346
			public Transform bendGoalRightArm;

			// Token: 0x04000543 RID: 1347
			public VRAimingController.BoneRotationOffset[] boneRotationOffsets;
		}

		// Token: 0x020000CF RID: 207
		[Serializable]
		public enum Handedness
		{
			// Token: 0x04000545 RID: 1349
			Right,
			// Token: 0x04000546 RID: 1350
			Left
		}

		// Token: 0x020000D0 RID: 208
		[Serializable]
		public class BoneRotationOffset
		{
			// Token: 0x04000547 RID: 1351
			public Transform transform;

			// Token: 0x04000548 RID: 1352
			public Vector3 value;
		}
	}
}
